<?php

class TinhThanh_Model extends MyModel
{
    
    function __construct()
    {
        parent::__construct();
    }
    
    function getTinhThanh($maTinhThanh)
    {
        $str = "SELECT matinhthanh, tentinhthanh 
                FROM tinhthanh 
                WHERE matinhthanh = '{$maTinhThanh}'";
                
        $query = mysql_query($str);
        return $query;
    }
    
    function getAllTinhThanh()
    {
        $str = "SELECT matinhthanh, tentinhthanh 
                FROM tinhthanh";
                
        $query = mysql_query($str);
        return $query;        
    }
    
    function checkExist($action, $maTinhThanh, $tenTinhThanh)
    {
        switch($action)
        {
            case "add":
                $str = "SELECT * 
                        FROM tinhthanh 
                        WHERE matinhthanh = '{$maTinhThanh}'";
                        
                $query = mysql_query($str);
                $result = mysql_num_rows($query) > 0 ? true : false;
                if($result == false)
                {
                    $str = "SELECT * 
                            FROM tinhthanh 
                            WHERE tentinhthanh = '{$tenTinhThanh}'";
                            
                    $query = mysql_query($str);
                    return mysql_num_rows($query) > 0 ? true : false;            
                }
                else 
                    return true;
                break;
                
            case "edit":
                $str = "SELECT * 
                        FROM tinhthanh 
                        WHERE tentinhthanh = '{$tenTinhThanh}'";
                        
                $query = mysql_query($str);
                return mysql_num_rows($query) > 0 ? true : false;
                break;
        }
    }
    
    function addTinhThanh($maTinhThanh, $tenTinhThanh)
    {
        $str = "INSERT INTO tinhthanh 
                VALUES('{$maTinhThanh}', '{$tenTinhThanh}')";
                
        mysql_query($str);
    }
    
    function editTinhThanh($maTinhThanh, $tenTinhThanh)
    {
        $str = "UPDATE tinhthanh 
                SET tentinhthanh = '{$tenTinhThanh}' 
                WHERE matinhthanh = '{$maTinhThanh}'";
                
        mysql_query($str);
    }
    
    function checkTinhThanhStatus($maTinhThanh)
    {
        $str = "SELECT matinhthanh 
                FROM quanhuyen 
                WHERE matinhthanh = '{$maTinhThanh}'";
                
        $query = mysql_query($str);
        return mysql_num_rows($query) > 0 ? true : false;
    }
    
    function deleteTinhThanh($maTinhThanh)
    {
        $str = "DELETE FROM tinhthanh 
                WHERE matinhthanh = '{$maTinhThanh}'";
                
        mysql_query($str);
    }
}

?>